﻿using DB.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RegistUserController : ControllerBase
    {
        // GET: api/<RegistUserController>
        [HttpGet]
        public List<RegistUser> Get()
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.RegistUsers
                    .Include(o=>o.Role)
                        .ThenInclude(o => o.RoleComparePageControls)
                    .Include(o=>o.Department).ToList();
                return data;
            }
        }

        // GET api/<RegistUserController>/5
        [HttpGet("{id}")]
        public RegistUser Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.RegistUsers
                    .Where(o => o.Id == id)
                    .Include(o => o.Role)
                        .ThenInclude(o => o.RoleComparePageControls)
                    .Include(o => o.Department)
                    .SingleOrDefault();
                return data;
            }
        }

        // POST api/<RegistUserController>
        [HttpPost]
        public void Post([FromBody] RegistUser value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.RegistUsers.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<RegistUserController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] RegistUser value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var user = db.RegistUsers.Where(o => o.Id == value.Id).SingleOrDefault();
                if (user != null)
                {
                    user.LoginName = value.LoginName;
                    user.LoginPassword = value.LoginPassword;
                    user.RoleId = value.RoleId;
                    user.DepartmentId = value.DepartmentId;
                    user.UpdateTime = DateTime.Now;
                    db.SaveChanges();
                }
            }
        }

        // DELETE api/<RegistUserController>/5
        [HttpDelete("{id}")]
        public void Delete(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var user = db.RegistUsers.Where(o => o.Id == id).SingleOrDefault();
                if (user != null)
                {
                    db.RegistUsers.Remove(user);
                    db.SaveChanges();
                }
            }
        }
    }
}
